﻿//Copyright (c) 2010 Christopher E. S. King (http://chriskingconsulting.com)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
// (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do 
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Concurrent;
using System.Threading;
using System.Runtime.Remoting;
using System.Threading.Tasks;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Diagnostics;
using Roslyn.Compilers.CSharp;
using System.Text.RegularExpressions;

using King.Declarative;
using King.Text;
using King.Reflection;
using King.Extensions;
using King.Collections;
using King.Traverse;
using King.CSharp.Reflection;


namespace King.CSharp.Text {

    internal sealed class CSharpSymbolAttribute : Attribute {
        private IEnumerable<SyntaxKind> m_kinds;
        internal CSharpSymbolAttribute(params SyntaxKind[] kinds) {
            m_kinds = kinds;
        }
        internal IEnumerable<SyntaxKind> SyntaxKinds() {
            return m_kinds;
        }
    }
    internal static class CSharpLoader {

        private static Dictionary<Type, CSharpNodeInfo> m_infoByType;
        private static Dictionary<SyntaxKind, CSharpNodeInfo> m_infoBySyntaxKind;

        static CSharpLoader() {
            m_infoByType =
               (from o in Assembly.GetExecutingAssembly().GetTypes()
                where o.IsDefined<CSharpSymbolAttribute>()
                select new CSharpNodeInfo(o))
                .ToDictionary(o => o.Type);

            m_infoBySyntaxKind =
               (from info in m_infoByType.Values
                from kind in info.Kinds()
                select new { info, kind })
                .ToDictionary(o => o.kind, o => o.info);
        }

        internal static CSharpNodeInfo GetNodeInfo(Type type) {
            return m_infoByType[type];
        }
        internal static CSharpNodeInfo TryGetNodeInfo(SyntaxKind kind) {
            return m_infoBySyntaxKind.GetValueOrDefault(kind);
        }
    }
    internal class CSharpNodeInfo {

        private Type m_type;
        private IEnumerable<SyntaxKind> m_kinds;

        internal CSharpNodeInfo(Type type) {
            m_type = type;

            var attr = type.GetAttribute<CSharpSymbolAttribute>();
            m_kinds = attr.SyntaxKinds();
        }

        internal Type Type {
            get { return m_type; }
        }
        internal bool IsToken {
            get { return Type.IsAssignableFrom(typeof(CSharpToken)); }
        }
        internal bool IsNode {
            get { return Type.IsAssignableFrom(typeof(CSharpNodeSyntax)); }
        }
        internal bool IsTrivia {
            get { return Type.IsAssignableFrom(typeof(CSharpTrivia)); }
        }
        internal IEnumerable<SyntaxKind> Kinds() {
            return m_kinds;
        }

        public override string ToString() {
            return m_type.Name;
        }
    }
}
